//
//  ChangePswByPhoneNumViewController.m
//  WangLottery
//
//  Created by 黄修勇 on 12-5-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ChangePswByPhoneNumViewController.h"

@interface ChangePswByPhoneNumViewController ()

-(void)hiddenKeybored;

@end

@implementation ChangePswByPhoneNumViewController

@synthesize verNumTxtFiled=_verNumTxtFiled;
@synthesize newPswTxtFiled=_newPswTxtFiled;
@synthesize verNewPswTxtFiled=_verNewPswTxtFiled;


-(void)dealloc{

    [_verNewPswTxtFiled release];
    [_verNumTxtFiled release];
    [_newPswTxtFiled release];
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title=@"修改密码";
    
    // Do any additional setup after loading the view from its nib.
}


-(IBAction)submit:(id)sender{

    [self hiddenKeybored];
    
    if (_verNumTxtFiled.text==nil||_verNumTxtFiled.text.length==0) {
        [self showAlertViewWithTitle:@"提示" msg:@"验证码不能为空!"];
        return;
    }
    
    if (_newPswTxtFiled.text==nil||_newPswTxtFiled.text.length<8) {
        [self showAlertViewWithTitle:@"提示" msg:@"新密码不能少于八位!"];
        return;
    }
    
    if (_verNewPswTxtFiled.text==nil||_verNewPswTxtFiled.text.length<8) {
        [self showAlertViewWithTitle:@"提示" msg:@"重复密码不能少于八位!"];
        return;
    }
    
    if (![_verNewPswTxtFiled.text isEqualToString:_newPswTxtFiled.text]) {
        [self showAlertViewWithTitle:@"提示" msg:@"两次输入的密码不一致"];
        return;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)hiddenKeybored{

    [_newPswTxtFiled resignFirstResponder];
    [_verNumTxtFiled resignFirstResponder];
    [_verNewPswTxtFiled resignFirstResponder];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"chang psw by phone num"] autorelease];
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.backgroundColor=[UIColor clearColor];
    cell.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f];
    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(80.0f, 45.0f/2-31.0f/2+4.0f, cell.contentView.frame.size.width-90.0f, 31.0f)];
    textField.delegate=self;
    textField.tag=indexPath.row;
    textField.clearButtonMode=UITextFieldViewModeWhileEditing;
    
    switch ([indexPath row]) {
        case 0:
            cell.textLabel.text=@"手机号";

            break;
        case 1:
            cell.textLabel.text=@"验证码";
            textField.placeholder=@"输入验证码";
            textField.keyboardType=UIKeyboardTypeNumberPad;
            textField.returnKeyType=UIReturnKeyNext;
            self.verNumTxtFiled=textField;
            break;   
        case 2:
            cell.textLabel.text=@"新密码";
            textField.placeholder=@"8-20个数字或者英文字母";
            textField.secureTextEntry=YES;
            textField.returnKeyType=UIReturnKeyDone;
            self.newPswTxtFiled=textField;
            break;   
        case 3:
            cell.textLabel.text=@"确认密码";
            textField.placeholder=@"再输一遍密码";
            textField.secureTextEntry=YES;
            self.verNumTxtFiled=textField;
            break;  
        default:
            break;
    }
    
    textField.autoresizingMask=UIViewAutoresizingFlexibleWidth;
    [cell.contentView addSubview:textField];
    [textField release];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self hiddenKeybored];
}
#pragma mark- 

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    if (textField.tag==0) {
        return NO;
    }else {
        return YES;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    if (textField.tag==2) {
        [_verNewPswTxtFiled becomeFirstResponder];
    }else {
        [textField resignFirstResponder];
    }
    return YES;
}
@end
